import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Sku from '../views/Sku.vue'
import Order from '../views/Order.vue'
Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'login',
  component: Login
}, {
  path: '/home',
  component: Home,
  redirect: {
    name: 'sku'
  },
  children: [{
    path: '/sku',
    name: 'sku',
    component: Sku
  }, {
    path: '/order',
    component: Order
  }]
}]

const router = new VueRouter({
  routes,
  mode: 'history'
})

export default router

router.beforeEach(function(to, from, next) {
  console.log(to);
  if (to.name == 'login') return next()

  let apiKey = localStorage.getItem("apiKey");
  if (!apiKey) return next('/')

  next()

  /* if (apiKey == null || apiKey == undefined || apiKey == '') {
    next('/')
  } else {
    next()
  }

  if (to.name == '/') {
    next()
  } */
})
